<?php 
	include "../service/util/authenticateSession.php";
	$url = ($_SERVER['REQUEST_URI']);
	//!preg_match('/^keyword=\w*&page=[1-9]{1,}[0-1]*$/', substr($url, strrpos($url, "?") + 1, strlen($url)))
	if (strpos($url, "?") == true &&
		(!preg_match('/^keyword=\w*$/', substr($url, strrpos($url, "?") + 1, strlen($url))) )
		){
		header("Location: index.php");
		exit;
	}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
	<title>Tài Khoản Người Dùng</title>
	<?php 
		include("import.php");
		include_once("../service/userService.php");
	?>
</head>
<body>
<?php include "header.php"; ?>
<form id="form" method="get">
<!-- Container -->
<div id="container">
    <div class="shell"> 
        <!-- Main -->
        <div id="main">
            <div class="cl">&nbsp;</div>
            <!-- Content -->
            <div id="content">
                <!-- Box -->
                <div class="box">
                    <!-- Box Head -->
                    <div class="box-head">
                        <h2 class="left">Danh Sách Người Dùng</h2> 
                        <div class="right">
                            <label>Tìm Kiếm:</label>
                            <input type="text" name="keyword" class="field small-field" onblur="submitForm('form','user.php', 'get');"/>
                        </div>
                    </div>
                    <!-- End Box Head -->	    
                    <!-- Table -->
                    <div class="table">
                        <table width="100%" border="0" cellspacing="0" cellpadding="0">
                            <tr>
                                <th>STT</th>
                                <th>Tên Đăng Nhập</th>
                                <th>Số Hóa Đơn</th>
                                <th>Họ và Tên</th>
                                <th>Ngày Sinh</th>
                                <th>Số Điện Thoại</th>
                                <th>Địa Chỉ</th>
                            </tr>
                            <?php
                                if (isset($_GET['page'])) $page = $_GET['page'];else $page = 1;
					
								$rowPerPage = 10;
								$keyword = "";
								if (isset($_GET['keyword'])) $keyword = $_GET['keyword'];
					
								$total = countUserBy($keyword);
								$totalPage = ceil($total / $rowPerPage);
								$startPosition = ($page * $rowPerPage) - ($rowPerPage);
                                $result = findAllUsersBy($keyword, $startPosition, $rowPerPage);
                                $count = 0;
                                while($row = ConnectionManager::getInstance()->fetch($result)){
                                    $count++;
                            ?>
                            <tr>
                                <td><?php echo $count; ?></td>
                                <td><h3><a href="#" onclick="submitForm('form', 'user-detail.php?id=<?php echo $row["USER_ID"]; ?>', 'post');"><?php echo $row["USER_NAME"]; ?></a></h3></td>
                                <td><?php echo $row["nReceipts"]; ?></td>
                                <td><?php echo $row["USER_FULLNAME"]; ?></td>
                                <td><?php echo date_format(date_create($row["USER_DOB"]), "d/m/Y"); ?></td>
                                <td><?php echo $row["USER_PHONENUMBER"]; ?></td>
                                <td><?php echo $row["USER_ADDRESS"]; ?></td>
                            </tr>
                            <?php 
                                }
                            ?>
                        </table>
                        <!-- Pagging -->
                        <div class="pagging">
                            <div class="left">Trang <?php echo(" ".$page." trong tổng số ".$totalPage." trang "); ?></div>
                            <div class="right">
                                <?php
                                for ($i = 1; $i <= $totalPage; $i++) {
                                    echo("<a href='"."user.php?"."keyword=".$keyword."&page=".$i."'>".$i."</a>");
                                }
                                ?>
                            </div>
                        </div>
                        <!-- End Pagging -->
                    </div>
                    <!-- Table -->
                </div>
                <!-- End Box -->
            </div>
            <!-- End Content -->
            <div class="cl">&nbsp;</div>			
        </div>
        <!-- Main -->
    </div>
</div>
<!-- End Container -->
</form>
<?php 
	include "footer.php";
	ConnectionManager::getInstance()->closeConnection(); 
?>
</body>
</html>